/*
 *  Copyright 2019-2025 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package cn.com.cdb.ldms;

import cn.com.cdb.ldms.annotation.rest.AnonymousGetMapping;
import cn.com.cdb.ldms.utils.SpringBeanHolder;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RestController;

/**
 * 开启审计功能 -> @EnableJpaAuditing
 *
 * @author jizi
 * @date 2025-06-24
 */
@Slf4j
@EnableAsync
@EnableScheduling
@RestController
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = {
        "cn.com.cdb.ldms",
//        "com.xxl.job"
})
@MapperScan(basePackages = {
        "cn.com.cdb.ldms.**.mapper"
//        , "com.xxl.job.admin.dao"
})
public class AppRun extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(AppRun.class);
        // 监控应用的PID，启动时可指定PID路径：--spring.pid.file=/home/ldms/app.pid
        // 或者在 application.yml 添加文件路径，方便 kill，kill `cat /home/ldms/app.pid`
        springApplication.addListeners(new ApplicationPidFileWriter());
        final ConfigurableApplicationContext run = springApplication.run(args);
        Environment env = run.getEnvironment();

        log.info("---------------------------------------------");
        final String serverPort = env.getProperty("server.port");
        log.info("Local: http://localhost:{}", serverPort);
        log.info("Swagger: http://localhost:{}/doc.html", serverPort);
        log.info("---------------------------------------------");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(AppRun.class);
    }

    @Bean
    public SpringBeanHolder springContextHolder() {
        return new SpringBeanHolder();
    }

    /**
     * 访问首页提示
     *
     * @return /
     */
    @AnonymousGetMapping("/")
    public String index() {
        return "Backend service started successfully";
    }
}
